<template>
  <div class="dateSteps">
    <div class="search_time">
      <el-date-picker
        v-model="curTime"
        type="date"
        format="YYYY-MM-DD"
        value-format="YYYY-MM-DD"
        placeholder="请选择时间"
        @change="handleTimeChange"
      />
    </div>
    <div class="search_btn">
      <el-button icon="ArrowLeft" @click="changTime('subtract')"></el-button>
      <span @click="changTimeToCur">当前日期</span>
      <el-button icon="ArrowRight" @click="changTime('add')"></el-button>
    </div>
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { ref } from 'vue'

const emit = defineEmits(['changeTime'])

const curTime = ref(dayjs().format('YYYY-MM-DD'))

const changTime = (type) => {
  if (type === 'subtract') {
    curTime.value = dayjs(curTime.value).subtract(1, 'day').format('YYYY-MM-DD')
  } else {
    curTime.value = dayjs(curTime.value).add(1, 'day').format('YYYY-MM-DD')
  }
  handleTimeChange()
}

const changTimeToCur = () => {
  curTime.value = dayjs(new Date()).format('YYYY-MM-DD')
  handleTimeChange()
}

const handleTimeChange = () => {
  emit('changeTime', curTime.value)
}
</script>

<style lang="scss" scoped>
.dateSteps {
  display: flex;
  align-items: center;
  .search_time {
    span {
      margin-right: 20px;
    }
  }
  .search_btn {
    display: flex;
    align-items: center;
    margin-left: 10px;
    span {
      cursor: pointer;
      padding: 4px 20px;
      border: 1px solid #dcdfe6;
      margin: 0 10px;
      border-radius: 2px;
    }
  }
}
</style>
